package com.lishe.lishe.lishe.common;

import android.app.Activity;
import android.content.Context;

import com.lishe.lishe.lishe.utils.LogUtil;

import java.util.Stack;

/**
 * 用于管理Activity和退出程序
 *
 * @author Bin
 */
public class AppManager {

    private static Stack<Activity> activityStack;
    private static AppManager instance;

    private AppManager() {
    }

    /**
     * 单一实例
     */
    public static AppManager getAppManager() {
        if (instance == null) {
            instance = new AppManager();
        }
        return instance;
    }

    /**
     * 获取栈
     */
    public static Stack<Activity> getActivityStack() {
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
        return activityStack;
    }

    /**
     * 添加Activity到堆栈
     */
    public void addActivity(Activity activity) {
        if (activityStack == null) activityStack = new Stack<Activity>();
        activityStack.add(activity);
    }

    /**
     * 获取当前Activity
     */
    public Activity currentActivity() {
        Activity activity = activityStack.lastElement();
        return activity;
    }

    /**
     * 结束当前Activity
     */
    public void finishActivity() {
        Activity activity = activityStack.lastElement();
        boolean isfinish = activity.isFinishing();
        LogUtil.i("111111111", activity.getLocalClassName() + "......." + isfinish);
        if (isfinish) {
            activityStack.remove(activityStack.size() - 1);
            finishActivity();
        } else {
            activityStack.get(activityStack.size() - 1).finish();
            activityStack.remove(activityStack.size() - 1);
        }
    }

    /**
     * 关闭指定Activity（包括该Activity后面的）
     */
    public void back2Activty(Class<?> cls) {
        if (!isInclude(cls)) return;
        try {
            for (int i = activityStack.size() - 1; i > 0; i--) {
                if (activityStack.get(i).getClass().equals(cls)) {
                    break;
                } else {
                    activityStack.get(activityStack.size() - 1).finish();
                    activityStack.remove(activityStack.size() - 1);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭指定Activity（包括该Activity后面的）
     */
    public void removeAssignActivity(Class<?> cls) {
        if (!isInclude(cls)) return;
        try {
            int a = activityStack.size();
            for (int i = a - 1; i > 0; i--) {
                if (activityStack.get(i).getClass().equals(cls)) {
                    finishActivity();
                    break;
                } else {
                    finishActivity();
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 判断Activity存不存在与集合里
     */
    public boolean isInclude(Class<?> cls) {
        for (int i = 0; i < activityStack.size(); i++) {
            if (activityStack.get(i).getClass() == cls) return true;
        }
        return false;
    }

    /**
     * 结束指定的Activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null) {
            activityStack.remove(activity);
            activity.finish();
        }
    }

    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls) {
        try {
            for (Activity activity : activityStack) {
                if (activity.getClass().equals(cls)) finishActivity(activity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        for (int i = 0, size = activityStack.size(); i < size; i++) {
            if (null != activityStack.get(i)) activityStack.get(i).finish();
        }
        activityStack.clear();
    }

    /**
     * 退出应用程序
     */
    public void exitApp() {
        try {
            finishAllActivity();
            /*
			 * ActivityManager activityMgr = (ActivityManager) context
			 * .getSystemService(Context.ACTIVITY_SERVICE);
			 */
			/*
			 * activityMgr.killBackgroundProcesses(context.getPackageName());
			 * System.exit(0);
			 */
            android.os.Process.killProcess(android.os.Process.myPid()); // 获取PID
            System.exit(0); // 常规java、c#的标准退出法，返回值为0代表正常退出
        } catch (Exception e) {
        }
    }

}